﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using OTA.Notification.BO.Template;
using OTA.Notification.Entity.Template;
using Strive.DataAccess.Base.BO;


using OTA.Notification.BO.Dictionary;
using OTA.Notification.Entity.Dictionary;

using System.Collections.Generic;

namespace OTA.Notification.Web.Template
{

    public class TemplateBO : MsgTemplateBO
    {

        protected override void ValidationRules()
        {
            //
        }
    }

    public partial class Template : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitPage();

                if (Request.QueryString["tcode"] != null)
                {
                    string strID = Request.QueryString["tcode"];

                    BindData(strID);
                }

            }
        }

        private void InitPage()
        {

            //类型加载

            using (DictionaryBO bo = new DictionaryBO())
            {

                IList<OTA_DICTIONARY> lstType = bo.getAllDictionary("Send", OTA.Notification.Common.DicType.Message, OTA.Notification.Common.LanguageType.Chinese);

                ddlMsgSendType.Items.Insert(0, new ListItem("--发送类型--", ""));

                foreach (OTA_DICTIONARY dic in lstType)
                    ddlMsgSendType.Items.Add(new ListItem(dic.NAME, dic.VALUE.ToString()));
            }

        }

        private void BindData(string ID)
        {

            string TemplateCode = "";
            string MsgTypeCode = "";

            string [] sp = ID.Split('_');

            if (sp.Length > 1)
            {
                TemplateCode = sp[0];

                
                MsgTypeCode = ID.Substring(TemplateCode.Length + 1, ID.Length - TemplateCode.Length - 1);

            }
            else
                return;

            MsgTemplateConfigBean bean = new MsgTemplateConfigBean();

            using (TemplateBO bo = new TemplateBO()) {

                bean = bo.SelectByPK(TemplateCode, MsgTypeCode);

                if (bean != null)
                {
                    txtName.Text = bean.TemplateName;
                    txtTitle.Text = bean.TemplateTitle;
                    txtContent.Text = bean.Content;
                    txtFlag.Text = bean.Flag;

                    txtTemplateCode.Text = bean.TemplateCode;

                    ddlMsgSendType.SelectedValue = bean.SendTypeCode;

                    txtTemplateCode.Enabled = false;

                    ddlMsgSendType.Enabled = false;
                }
            }

        }


        protected void btnSave_Click(object sender, EventArgs e)
        {

            if (string.IsNullOrEmpty(txtTemplateCode.Text))
            {
                Response.Write("<script>alert('请输入模板编号！')</script>");
                return;
            }

            if (string.IsNullOrEmpty(txtName.Text))
            {
                Response.Write("<script>alert('请输入模板名称！')</script>");
                return;
            }

            if (string.IsNullOrEmpty(txtContent.Text))
            {
                Response.Write("<script>alert('请输入模板内容！')</script>");
                return;
            }

            if (string.IsNullOrEmpty(txtFlag.Text))
            {
                Response.Write("<script>alert('请输入模板标识！')</script>");
                return;
            }


            using (TemplateBO bo = new TemplateBO())
            {
                MsgTemplateConfigBean bean = new MsgTemplateConfigBean();

                if (Request.QueryString["tcode"] == null && Request.QueryString["mscode"] == null)
                {
                    bean.CreateTime = DateTime.Now;

                    //new mode

                    //防止重复判断
                    MsgTemplateConfigBean mb = bo.SelectByPK(Request.QueryString["tcode"], Request.QueryString["mscode"]);

                    if (mb != null)
                    {
                        Response.Write("<script>alert('此模板已存在！')</script>");

                        bo.Dispose();

                        return;
                    }

                    bean.SendTypeCode = ddlMsgSendType.SelectedValue;

                }
                else
                {
                    //edit mode

                    string strKey = Request.QueryString["tcode"];

                    int iEnd = strKey.LastIndexOf('_');

                    string strTemplateCode = strKey.Substring(0, iEnd);

                    string strMsgTypeCode = strKey.Substring(iEnd + 1, strKey.Length - iEnd - 1);

                    bean = bo.SelectByPK(strTemplateCode, strMsgTypeCode);

                    bo.MarkOld();

                    bean.SendTypeCode = strMsgTypeCode;
                    
                }

                bean.TemplateCode = txtTemplateCode.Text;
                

                bean.TemplateName = txtName.Text;
                bean.TemplateTitle = txtTitle.Text;
                bean.Content = txtContent.Text;
                bean.Flag = txtFlag.Text;

                bean.LastUpdateTime = DateTime.Now;

                bo.Save(bean);
            }

            Response.Write("<script>window.parent.Close()</script>");
        }
    }
}
